﻿using System;
using System.Collections.Generic;

namespace Solution.Comm.Extensions
{
    public static class CollectionExt
    {
        #region 判断集合是否为空

        public static bool IsNullOrEmpty<T>(this ICollection<T> source)
        {
            return source == null || source.Count <= 0;
        }

        #endregion 判断集合是否为空

        #region 判断Guid类型是否为Empty

        public static bool IsEmpty(this Guid guid)
        {
            return guid == Guid.Empty;
        }

        #endregion 判断Guid类型是否为Empty

        #region 根据实体属性自定义去重

        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
            Func<TSource, TKey> keySelector)
        {
            var seenKeys = new HashSet<TKey>();
            foreach (var element in source) {
                if (seenKeys.Add(keySelector(element))) {
                    yield return element;
                }
            }
        }

        #endregion 根据实体属性自定义去重
    }
}